//
// Created by mio on 25-4-23.
//

#ifndef RTMPSERVER_H
#define RTMPSERVER_H

#include <mutex>
#include <RtmpSession.h>

#include "../Net/TcpServer.h"
#include "rtmp.h"
#include "RtmpSession.h"

class RtmpServer : public TcpServer, public Rtmp, public std::enable_shared_from_this<RtmpServer>
{
public:
    using EventCallback = std::function<void(std::string type, std::string streampath)>;
    // 创建服务器
    static std::shared_ptr<RtmpServer> Create(EventLoop* eventloop); // 单例
    ~RtmpServer();

    void SetEventCallback(const EventCallback& cb);

private:
    RtmpServer(EventLoop* eventloop);
    void AddSession(std::string stream_path); // 每个Session由路径来区分
    void RemoveSession(std::string stream_path);

    RtmpSessionPtr GetSession(std::string stream_path);
    bool HasPublisher(std::string stream_path);
    bool HasSession(std::string stream_path);
    void NotifyEvent(std::string type, std::string stream_path);

    TcpConnectionPtr OnConnect(int sockfd) override;

    friend class RtmpConnection;

    EventLoop* loop_;
    std::mutex mtx_;
    std::unordered_map<std::string, RtmpSessionPtr> rtmp_sessions_;
    std::vector<EventCallback> event_callbacks_;
};

#endif //RTMPSERVER_H